package com.frxuexi.visit.view

import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.entity.StatusBean
import com.frxuexi.base.entity.VisitBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.base.utils.ArouterManager
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.adapter.VisitAdapter
import com.frxuexi.visit.adapter.VisitStatusAdapter
import com.frxuexi.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitIntent, VisitViewModel>() {

    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }
    var position=-1
    var purpose=""
    override fun getVM(): VisitViewModel {
        return viewModels<VisitViewModel>().value
    }

    override fun getBind(): ActivityVisitBinding {
        return ActivityVisitBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                visitAdapter.data.clear()
                                val items = it.data as Collection<VisitBeanItem>
                                if(items.size!=0) {
                                    visitAdapter.addData(items.sortedByDescending { it.id })
                                    visitAdapter.notifyDataSetChanged()
                                }else{
                                    vm.sendIntent(VisitIntent.getVisitByPurpose(purpose))
                                }

                            }
                            RequestType.DELETE->{
                                visitAdapter.notifyItemRemoved(position)
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val statusBeans = listOf<StatusBean>(
            StatusBean("全部状态", true),
            StatusBean("待审核", false),
            StatusBean("已通过", false),
            StatusBean("驳回", false),
            StatusBean("已完成", false)
        )
        visitStatusAdapter.addData(statusBeans)
        binding.visitStatusRv.apply {
            adapter=visitStatusAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity,RecyclerView.HORIZONTAL,false)
        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck=false
            }
            visitStatusAdapter.data.get(position).isCheck=true
            visitStatusAdapter.notifyDataSetChanged()

            if(position==0){
                vm.sendIntent(VisitIntent.getVisits)
            }else{
                vm.sendIntent(VisitIntent.getVisitsByStatus(statusBeans.get(position).title))
            }
        }

        binding.visitRv.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }

        ItemTouchHelper(object :SimpleCallback(0, LEFT){
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
                vm.sendIntent(VisitIntent.deleteVisit(visitAdapter.data.get(position).id!!))

            }
        }).attachToRecyclerView(binding.visitRv)

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.navigation(this,"/visit/VisitDetailActivity","vid",visitAdapter.data.get(position).id.toString())
        }

        visitAdapter.addChildClickViewIds(R.id.visit_update)
        visitAdapter.setOnItemChildClickListener { adapter, view, position ->
            ArouterManager.navigation(this,"/visit/VisitAddActivity","vid",visitAdapter.data.get(position).id!!.toString())
        }


        binding.visitSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.sendIntent(VisitIntent.getVisitByName(query!!))
                purpose=query!!
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                vm.sendIntent(VisitIntent.getVisits)
                return true
            }
        })

        binding.visitAdd.setOnClickListener {
            ArouterManager.navigation(this,"/visit/VisitAddActivity","vid")
        }


    }

    override fun onResume() {
        super.onResume()
        val title = visitStatusAdapter.data.filter { it.isCheck }.get(0).title
        if(title=="全部状态") {
            vm.sendIntent(VisitIntent.getVisits)
        }else{
            vm.sendIntent(VisitIntent.getVisitsByStatus(title))
        }
    }

}